package com.jl.book.ui.base

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking

/**
 *@author jl
 */
class SplashActivity : AppCompatActivity() {

    val permission :MutableList<String> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        XXPermissions.with(this)
            .permission(Permission.WRITE_CALL_LOG)
            .permission(Permission.READ_CALL_LOG)
            .permission(Permission.READ_CONTACTS)
            .permission(Permission.WRITE_CONTACTS)
            .permission(Permission.CALL_PHONE)
            .permission(Permission.SEND_SMS)
            .request { permissions, all ->
                if (all){
                    jump()
                }else{
                    permission.addAll(permissions)
                    XXPermissions.startPermissionActivity(this,permissions)
                }
            }
    }

    override fun onResume() {
        super.onResume()
        if (permission.size != 0){
            if (XXPermissions.isGrantedPermission(this,permission)){
                jump()
            }
        }
    }

    private fun jump() {
        runBlocking {
            delay(2000)
        }
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }
}
